package com.caigou.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jiliang.purchase.demo.CriteriaTaiZhang;
import jiliang.purchase.demo.PersonObject;
import jiliang.purchase.demo.Tongjitaizhangdemo;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import com.caigou.service.getTaiZhangSerice;

public class TaiZhangExport extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */

	private getTaiZhangSerice GTZ = new getTaiZhangSerice();
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel;charset=utf-8");

		String filedisplay = "liebiao.xls";
		response.addHeader("Content-Disposition", "attachment;filename="+ filedisplay);
		PersonObject PO = new PersonObject();
		PO = (PersonObject)session.getAttribute("Person");
		
		String xmid = null;
		String xingming = null;
		String start = null;
		String endtime = null;
		String shenqingsj = null;
		String jinfeidmName = null;
		String last_lcName = null;
		String caigoufsName = null;

		String[] title = request.getParameterValues("title");

		
		if(request.getParameter("xmid")!=null && !request.getParameter("xmid").equals("0"))
		{
			xmid = request.getParameter("xmid");
		}
		//System.out.println(xmid);
		

		if(request.getParameter("xingming")!=null && !request.getParameter("xingming").equals(""))
		{
			xingming = request.getParameter("xingming");
			xingming = java.net.URLDecoder.decode(xingming, "UTF-8");
		}
		if(request.getParameter("jinfeidmName")!=null && !request.getParameter("jinfeidmName").equals("0"))
		{
			jinfeidmName = request.getParameter("jinfeidmName");
		}
		if(request.getParameter("shenqingsj")!=null && !request.getParameter("shenqingsj").equals("0"))
		{
			shenqingsj = request.getParameter("shenqingsj");
		}
		if(request.getParameter("last_lcName")!=null && !request.getParameter("last_lcName").equals("0"))
		{
			last_lcName = request.getParameter("last_lcName");
		}
		if(request.getParameter("caigoufsName")!=null && !request.getParameter("caigoufsName").equals("0"))
		{
			caigoufsName = request.getParameter("caigoufsName");
		}

		//System.out.println(xingming);
		if(request.getParameter("starttime")!=null && !request.getParameter("starttime").equals(""))
		{
			start = request.getParameter("starttime");
		}

		//System.out.println(start);
		if(request.getParameter("endtime")!=null && !request.getParameter("endtime").equals(""))
		{
			endtime = request.getParameter("endtime");
		}

		//System.out.println(endtime);
		CriteriaTaiZhang CTZ = new CriteriaTaiZhang("4",xingming,start,endtime,caigoufsName,jinfeidmName,last_lcName,shenqingsj);
		List<Tongjitaizhangdemo> list = new ArrayList<Tongjitaizhangdemo>();
		list = GTZ.gettaizhang(CTZ, title);
		
		try {
				writeExcel(response.getOutputStream(),list,title);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}
	
	public void writeExcel(OutputStream out,List<Tongjitaizhangdemo> list,String[] titles)throws Exception
	{
		Map<String,String> map = new HashMap<String,String>();
		List<String> title = new ArrayList<String>();
		map.put("think_shenqing.bh", "业务编号");
		map.put("xiangmumc", "项目名称 ");
		map.put("lianxiren", "联系人 ");
		map.put("lianxidh", "联系人手机 ");
		map.put("dw_mc", "使用部门 ");
		map.put("jingfeimc", "经费名称 ");
		map.put("jingfeidm", "经费代码");
		map.put("caigoufs", "采购方式 ");
		map.put("hejije", "合计金额（元）");
		map.put("sq_shijian", "申请时间 ");
		map.put("sh_riqi", "计财处审批时间 ");
		map.put("last_lc", "业务状态 ");
		WritableWorkbook book = Workbook.createWorkbook(out);
		WritableSheet sheet = book.createSheet("sheet1", 0);
		
		for(int i=0;i<titles.length;i++)
		{
			title.add(map.get(titles[i]));
		}

		
		for(int i=0;i<title.size();i++)
		{
			Label label = new Label(i,0,title.get(i));
			sheet.addCell(label);
		}
		
		Pattern pattern = Pattern.compile("([0-9]+)|([0-9]+)(\\.)([0-9]+)");
		for(int i=1;i<list.size();i++)//行
		{	
			/*	Label label1 = new Label(0,i,list.get(i).getBh());
				//jxl.write.Number number = new jxl.write.Number(i,0,Double.valueOf(list.get(i).getBh())); 
*/
				for(int j=0;j<titles.length;j++)
				{
					String value = list.get(i).getByName(titles[j]);
					Matcher matcher = pattern.matcher(value);
					if(matcher.matches())
					{
						jxl.write.Number number = new jxl.write.Number(j,i,Double.valueOf(value));
						sheet.addCell(number);
					}
					else
					{
						Label label = new Label(j,i,value);
						sheet.addCell(label);
					}
					
					
				}
		}		
			book.write();
			book.close();
	}

}
